package br.com.gsc.setup;

import java.sql.DriverManager;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Transactional;

import br.com.gsc.model.tableMapping.Admin;
import br.com.gsc.model.tableMapping.Company;
import br.com.gsc.model.tableMapping.Operator;
import br.com.gsc.model.tableMapping.Product;
import br.com.gsc.model.tableMapping.RoleUserType;
import br.com.gsc.model.tableMapping.User;
import br.com.gsc.repository.objRepos.CompanyRepository;
import br.com.gsc.repository.objRepos.PersonRepository;

@Transactional
@Configurable(preConstruction=true)
public class GscDBSetup {

	@Autowired
	private PersonRepository pr;
	
	@Autowired
	private CompanyRepository cr;

	private Company comp;

	private Product prod;
		
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		createSchemas();
		//Initialize the Spring application context in order to make dependency injection work
	    AbstractApplicationContext context = new ClassPathXmlApplicationContext("classpath*:META-INF/spring/applicationContext.xml");
	    context.registerShutdownHook();	    
		GscDBSetup gsc = new GscDBSetup();
		gsc.initDB();
	}
	
	public void initDB(){
		setupCompany(); 
		setupUsers(); 
	}
	
	public static void createSchemas(){
		try {
			Class.forName("com.mysql.jdbc.Driver");

			java.sql.Connection  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/","gsc","gsc");

			java.sql.Statement stdrop = con.createStatement();
			stdrop.executeUpdate("DROP SCHEMA IF EXISTS gsc ");
			
			java.sql.Statement stdropTest = con.createStatement();
			stdropTest.executeUpdate("DROP SCHEMA IF EXISTS gsctest ");
			
			java.sql.Statement st = con.createStatement();
			st.executeUpdate("CREATE SCHEMA gsc ");

			java.sql.Statement stTest = con.createStatement();
			stTest.executeUpdate("CREATE SCHEMA gsctest ");
			
		} catch (ClassNotFoundException e) {
			System.out.println("Erro na criação das databases");
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("Erro na criação das databases");
			e.printStackTrace();
		}
	}
	
	public void setupCompany(){	
		comp = new Company();
		comp.setName("testComp");
		setupProducts(comp);
		cr.addCompany(comp);
	}
	
	public void setupProducts(Company comp){
		prod = new Product();
		prod.setName("testProd");
		comp.addProduct(prod);
	}

	private void setupUsers() {
		 User user = new User();
		 user.setUsername("user");
		 user.setPassword("test");
		 pr.addPerson(user);
		 
		 Admin superAdmin = new Admin();
		 superAdmin.setUsername("Super");
		 superAdmin.setSuperAdmin(true);
		 superAdmin.addCompany(comp);
		 superAdmin.setPassword("test");
		 pr.addPerson(superAdmin);
		 
		 Admin admin = new Admin();
		 admin.setUsername("admin");
		 admin.setPassword("test");		 
		 admin.addCompany(comp);
		 pr.addPerson(admin);
		 
		 Operator oper = new Operator();
		 oper.setUsername("oper");
		 oper.setPassword("test");
		 pr.addPerson(oper);		 		 
	}

}
